<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('编辑')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="375px" @close='close' top="20vh">
    <div class="dialogContentBox">
      <el-form ref="form" :model="record" label-width="80px" label-position="left" :rules="rules" style="margin:0 20px;">
        <el-form-item :label="$t('名称')" prop="name">
          {{ record.name }}
        </el-form-item>
        <el-form-item :label="$t('状态')" prop="status">
          <el-radio-group v-model="record.status" @change="statusChange">
            <el-radio label="enable" class="enable">{{$t('允许')}}</el-radio>
            <el-radio label="disable" class="disable">{{$t('拒绝')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('端口')" prop="port">
          <el-input v-emoji v-model="record.port" ref="port" :disabled='record.status === "disable"' style="width:215px" size='small'></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { changeManagementPortInfo } from '@/api/safe/access/protect'
import { inputClass } from '@/utils/input-class.js'
import Cookies from 'js-cookie'
export default {
  name: 'ManagementDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (this.record.status === 'enable' && String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }

    return {
      dialogVisible: false,
      record: {
        name: '',
        status: 'enable',
        port: ''
      },
      recordCopy: {
        name: '',
        status: 'enable',
        port: ''
      },
      old_port: '',
      rules: {
        port: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data, name) {
      this.dialogVisible = true
      this.record.name = name
      this.record.status = data.status
      this.record.port = data.port
      this.old_port = data.port
      this.$nextTick(() => {
        const data = ['port']
        inputClass(this.$refs, data)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let time = (this.record.name === 'https' && this.old_port !== this.record.port) ? 500 : (600 * 1000)
          changeManagementPortInfo(this.record, time).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('执行成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          }).catch(e => {
            if (this.record.name === 'https' && this.old_port !== this.record.port && e.status !== -20) {
              const port = window.location.href.split('/')[2].split(':')[1] || 443
              const alert_id = Cookies.get('alert_id' + port)
              Cookies.set('alert_id' + this.record.port, alert_id)
              const system_key = Cookies.get('system_key' + port)
              Cookies.set('system_key' + this.record.port, system_key)
              const userInfo = Cookies.get('userInfo' + port)
              Cookies.set('userInfo' + this.record.port, userInfo)
              const roleList = Cookies.get('roleList' + port)
              Cookies.set('roleList' + this.record.port, roleList)
              const Oem = Cookies.get('Oem' + port)
              Cookies.set('Oem' + this.record.port, Oem)
              Cookies.set('old_port', port)
              let reg = new RegExp('(?<=http:\\/\\/|https:\\/\\/)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62}|(?=:[0-9]{1,6}))+\\.?', 'g')
              const hrefUrl = window.location.href.match(reg)
              let url = hrefUrl[0] + ':' + this.record.port + '/#/safe/localAccess'
              let path = window.location.protocol + '//' + url
              window.location.href = path
            }
          })
        }
      })
    },
    statusChange () {
      this.$refs.form.clearValidate('port')
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.dialogContentBox {
  max-height: calc(60vh - 184px);
  overflow-y: auto;
}
</style>
